﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SICOMModbus
{
    public class ModbusRTU_ADU
    {
        byte address;

        public byte Address
        {
            get { return address; }
            set { address = value; }
        }
        private byte functionCode;
        /// <summary>
        /// 功能码
        /// </summary>
        public byte FunctionCode
        {
            get { return functionCode; }
            set { functionCode = value; }
        }
        private byte[] data;
        /// <summary>
        /// 数据
        /// </summary>
        public byte[] Data
        {
            get { return data; }
            set { data = value; }
        }
        public byte[] getADUBytes()
        {
            byte[] Bytes = new byte[4 + data.Length];
            Bytes[0] = address;
            Bytes[1] = functionCode;
            for (int i = 2; i < Bytes.Length - 2; i++)
            {
                Bytes[i] = data[i - 2];
            }
            ushort x = CRC.CRC16(Bytes, 0, Bytes.Length - 2);
            Bytes[Bytes.Length - 2] = (byte)(x >> 8);
            Bytes[Bytes.Length - 1] = (byte)(x << 8 >> 8);
            return Bytes;
        }

    }
}
